package com.hnucm.cooklife.presenter.order;

import androidx.annotation.NonNull;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.OrderDetail;
import com.hnucm.cooklife.contract.OrderDetaildetail.Contract;
import com.hnucm.cooklife.model.OrderDetailModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class OrderDetailPresenter extends BasePresenter<Contract.IOrderDetailView> implements Contract.IOrderDetailPresenter {

    Contract.IOrderDetailModel mIOrderDetailModel;

    public OrderDetailPresenter() {
        mIOrderDetailModel = new OrderDetailModel();
    }

    @Override
    public void loadOrderDetail(Integer oId) {
        mIOrderDetailModel.loadOrderDetail(oId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderDetail>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull OrderDetail orderDetail) {
                        if(isViewAttached()){
                            getView().onLoadOrderDetail(orderDetail);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshOrderDetail(Integer oId) {

    }
}
